﻿using Kevin.Shop.Business.Core.Components.Cookie;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Common.Component;
using RiderShop.Mobile.Common.Component.OAuth.Weixing;
using RiderShop.Mobile.Modle.MyAccount;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace RiderShop.Mobile.Controllers
{
    public partial class PromotionController : ThemeController
    {
        [HttpGet]
        public ActionResult Index()
        {
            MyPromotionCodeModel myPromotionCodeModel = new MyPromotionCodeModel();

            return View("Index", myPromotionCodeModel);
        }

        [HttpGet]
        public ActionResult Mili20150116()
        {
            MyPromotionCodeModel myPromotionCodeModel = new MyPromotionCodeModel();

            return View("Mili20150116");
        }
        [HttpGet]
        public ActionResult Huifang20150121()
        {
            MyPromotionCodeModel myPromotionCodeModel = new MyPromotionCodeModel();

            return View("Huifang20150121");
        }

        /// <summary>
        /// 根据每个卡券编号的时间戳和签名
        /// </summary>
        /// <param name="cardId">卡券id</param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetTimeStampAndSignature(string cardId)
        {
            MyPromotionCodeModel myPromotionCodeModel = new MyPromotionCodeModel();
            myPromotionCodeModel.GetTimeStampAndSignature(cardId);

            return Json(new { cardId = myPromotionCodeModel.CardId, timestamp = myPromotionCodeModel.TimeStamp, signature = myPromotionCodeModel.Signature }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ShareLuckyBag(string pc)
        {
            if (string.IsNullOrEmpty(KevinCookieManager.Get("openid")) || string.IsNullOrEmpty(KevinCookieManager.Get("headimgurl")) || string.IsNullOrEmpty(KevinCookieManager.Get("nickname")))
            {
                var weixinInfo = WeixingOAuth.GetWeixinOAuthInfo();
                if (string.IsNullOrEmpty(weixinInfo.openid))
                {
                    return View();
                }
                KevinCookieManager.Set("openid", weixinInfo.openid, 1000000);
                KevinCookieManager.Set("headimgurl", weixinInfo.headimgurl, 1000000);
                KevinCookieManager.Set("nickname", weixinInfo.nickname, 1000000);

            }


            return View();
        }

        public ActionResult MyLuckyBag(string pc, string fid)
        {
            MyPromotionCodeModel model = new MyPromotionCodeModel();

            if (string.IsNullOrEmpty(KevinCookieManager.Get("openid")) || string.IsNullOrEmpty(KevinCookieManager.Get("headimgurl")) || string.IsNullOrEmpty(KevinCookieManager.Get("nickname")))
            {
                var weixinInfo = WeixingOAuth.GetWeixinOAuthInfo();
                if (string.IsNullOrEmpty(weixinInfo.openid))
                {
                    model.Message = "aaaaaaaa";
                    return View(model);
                }
                KevinCookieManager.Set("openid", weixinInfo.openid, 1000000);
                KevinCookieManager.Set("headimgurl", weixinInfo.headimgurl, 1000000);
                KevinCookieManager.Set("nickname", weixinInfo.nickname, 1000000);
            }
            var openid = KevinCookieManager.Get("openid");
            var headimgurl = KevinCookieManager.Get("headimgurl");
            var nickname = KevinCookieManager.Get("nickname");
            var res = model.ReceiveUserShardPrmotionCode(pc, "weixing_" + fid, "weixing_" + openid, headimgurl, nickname);
            if (res.Status == false)
            {
                model.Message = res.ErrorMessage;
            }

            model.GetUserSharedPrmotionCodes(0, 1000, pc, "weixing_" + fid);

            return View(model);
        }

        public JsonResult GetCustomer(string id)
        {
            string s = System.IO.File.ReadAllText(@"E:\WorkSpace\WebDemo\qingShu\custom\54c9e4c7a3664e163f0000c6\385892574b1fb2217d89cf494d0b6bd8.json");
            return Json(s, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult GetCustomer()
        {
            string s = System.IO.File.ReadAllText(@"E:\WorkSpace\WebDemo\qingShu\custom\54c9e4c7a3664e163f0000c6\385892574b1fb2217d89cf494d0b6bd8.json");
            return Json(s, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 领取优惠券页面（包含优惠券信息 & 有哪些限制）
        /// </summary>
        /// <param name="orderNumber">订单号</param>
        /// <param name="promotionCode">优惠券码</param>
        /// <returns></returns>
        public ActionResult PromotionCodeView(string promotionCode, string orderNumber = "")
        {
            MyPromotionCodeModel model = new MyPromotionCodeModel();
            model.GetPromotionCodeAndPromotionItems(promotionCode);

            return View(model);
        }

        /// <summary>
        /// 用户领取优惠券
        /// </summary>
        /// <param name="orderNumber">订单号</param>
        /// <param name="promotionCode">优惠券码</param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public ActionResult ReceivePrmotionCode(string promotionCode, string orderNumber = "")
        {
            if (string.IsNullOrEmpty(promotionCode))
            {
                return Json(new { Status = false, ErrorMessage = "noHasPromotionCode" }, JsonRequestBehavior.AllowGet);
            }

            MyPromotionCodeModel myPromotionCodeModel = new MyPromotionCodeModel();
            var result = myPromotionCodeModel.ReceivePrmotionCode(promotionCode, orderNumber);

            if (result.Status == true && string.IsNullOrEmpty(result.ErrorMessage))
            {
                 return Json(new { Status = true, ErrorMessage = "receiveSuccessfully", Tag = result.Tag }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Status = result.Status, ErrorMessage = result.ErrorMessage, Tag = result.Tag }, JsonRequestBehavior.AllowGet);
            }
        }
     
        /// <summary>
        /// 领取优惠券页面（面膜）
        /// </summary>
        /// <param name="promotionCode"></param>
        /// <returns></returns>
        public ActionResult PromotionCodeSpecial(string promotionCode)
        {
            MyPromotionCodeModel model = new MyPromotionCodeModel();
            model.GetPromotionCodeAndPromotionItems(promotionCode);

            return View(model);
        }

        /// <summary>
        /// 领取优惠券页面（特定活动）
        /// </summary>
        /// <param name="promotionCode"></param>
        /// <returns></returns>
        public ActionResult PromotionCodeSpecialActivity(string promotionCode)
        {
            MyPromotionCodeModel model = new MyPromotionCodeModel();
            model.GetPromotionCodeAndPromotionItems(promotionCode);
            bool isExisted = System.IO.File.Exists(Server.MapPath("~/Views/Promotion/SpecialActivity/" + promotionCode + ".cshtml"));
            //if (isExisted)
            //{
            //    return View("~/Views/Promotion/SpecialActivity/" + promotionCode + ".cshtml", model);
            //}
            //else
            //{
                return View("~/Views/Promotion/PromotionCodeActivity.cshtml", model);
            //}
        }
    }
}
